<?php

/**
 * 
 */
class Profile extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('routes_model');
        $this->load->model('users_model');
        $this->load->model('reports_model');
    }

    public function getUserStats() {
        $user_id = $this->session->userdata("sess_user_id");
        $routes = $this->routes_model->fetchRoutesByUserId($user_id);
        $data['count'] = $routes['pocet'];
        $data['kms'] = $routes['kms'];

        $reports = $this->users_model->fetchUserReportsCount($user_id);
        $data['reports'] = $reports;



        $this->load->view('profile/myStats', $data);
    }

    public function getUserReportsCountByLogin() {

        $login = $this->input->post('login');
        $reports = $this->users_model->fetchUserReportsCountByLogin($login);
        $data['reports'] = $reports;
        $this->load->view('profile/userStats', $data);
    }

    public function getUserReports() {
        
    }

    public function saveAreas() {
        $areas = $this->input->post('areas');
        $string = "";
        foreach ($areas as $key => $value) {
            $string .= $value . ",";
        }
        $string = substr_replace($string, '', -1);
        $user_id = $this->session->userdata("sess_user_id");
        $this->users_model->performUpdateAreas($string, $user_id);
    }

    public function saveKeywords() {
        $keywords = $this->input->post('keywords');
        $string = "";
        foreach ($keywords as $key => $value) {
            $string .= $value . ",";
        }
        $string = substr_replace($string, '', -1);
        $user_id = $this->session->userdata("sess_user_id");
        $this->users_model->performUpdateKeywords($string, $user_id);
    }

    public function newPass($hash) {
        $array = $this->users_model->checkHash($hash);
        $hesielko = $array['heslo'];
        $emailik = $array['pole']['email'];

        $config = Array(
            'protocol' => "smtp",
            'smtp_host' => "ssl://smtp.googlemail.com",
            'smtp_port' => 465,
            'smtp_user' => "soferujem@gmail.com",
            'smtp_pass' => "adampeto"
        );


        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from('soferujem@gmail.com', 'Soferujem');
        $this->email->to($emailik);
        $this->email->subject('Obnovené heslo');
        $this->email->message("Nove heslo mate: " . $hesielko . " zmente si ho!");
        $this->email->send();
    }

    public function keywordOccurences() {
        $keywords = $this->input->post('keyw_occ');
        $arr = $this->reports_model->checkOccurences($keywords);
        echo json_encode($arr['pocet']);
    }

}

?>